package com.hue.springbootrabbit.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 姚凤桥
 * @Date 2022/1/4 15:48
 * @Description: rabbitmq配置
 */
@Configuration
public class RabbitConfig {
    /**
     * 配置RabbitMq的消息转换器
     *
     * @return
     */
    @Bean
    public Jackson2JsonMessageConverter jackson2JsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    /**
     * 在容器启动时自动连接mq创建一个交换机(如果mq中没有该组件)
     *
     * @return
     */
    @Bean
    public Exchange exchange() {
        return new TopicExchange("test.exchange", true, false);
    }

    /**
     * 创建一个队列
     *
     * @return
     */
    @Bean
    public Queue queue() {
        return new Queue("test.queue");
    }

    /**
     * 创建一个绑定关系,将队列绑在交换机上
     *
     * @return
     */
    @Bean
    public Binding binding() {
        return new Binding("test.queue", Binding.DestinationType.QUEUE, "test.exchange", "test", null);
    }
}
